package com.servlets;

import com.alibaba.fastjson.JSON;
import com.dao.impl.ProductTypeDaoImpl;
import com.pojo.Classify;
import com.pojo.PageBean;
import com.pojo.Product;
import com.pojo.ProductType;
import com.service.BrandService;
import com.service.ProductService;
import com.service.impl.BrandSercviceImpl;
import com.service.impl.ProductServiceImpl;
import com.utils.JDBCUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@WebServlet("/product.do")
public class ProductSevlet extends BaseServlet{
    private ProductService service = new ProductServiceImpl();
    private  Classify classify = new Classify();

    public void pageAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String current = request.getParameter("current");
        String limit = request.getParameter("limit");
        Integer curr=1,lim = 10;
        if (current!=null)
            curr=Integer.parseInt(current);
        if (limit!=null)
            lim = Integer.parseInt(limit);
        PageBean<Product> page = service.findAll(lim,curr);
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(JDBCUtil.toLayuiJson(page.getTotal(),page.getData()));
    }

    public void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String id = request.getParameter("id"),msg;
        if (service.deleteP(id))
            msg = "删除成功";
        else
            msg = "删除失败";
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(JSON.toJSONString(msg));

    }

    public void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        Map<String, String[]> map = request.getParameterMap();
        Product product = JDBCUtil.mapToBean(map, Product.class);
        product.setProductBrand(new BrandSercviceImpl().findByName(product.getProductBrand()).getId());
        String msg;
        if(service.update(product))
            msg = "成功";
        else
            msg = "失败";
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(JSON.toJSONString(msg));
    }

    public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        Map<String, String[]> map = request.getParameterMap();
        Product product = JDBCUtil.mapToBean(map, Product.class);
        product.setId(UUID.randomUUID().toString().replace("-","").substring(0,10));
        product.setCreateTime(new Timestamp(new Date().getTime()));
        product.setProductBrand(new BrandSercviceImpl().findByName(product.getProductBrand()).getId());
        String msg;
        if(service.addP(product))
            msg = "成功";
        else
            msg = "失败";
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(JSON.toJSONString(msg));
    }

    public void findByID(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        Product product = service.findByID(id);
        request.getSession().setAttribute("product",product);
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(JSON.toJSONString(product));
    }

    public void newProduct(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Product> list = service.newProduct();
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(JSON.toJSONString(list));
    }

    public void sortProduct(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String productTypeName = request.getParameter("productTypeName");
        String limit = request.getParameter("limit");
        List<Product> list = service.sortProduct(productTypeName,limit);
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(JSON.toJSONString(list));
    }

    public void findProducts(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=utf-8");

        Map<String, String[]> map = request.getParameterMap();
        Classify classify = JDBCUtil.mapToBean(map, Classify.class);

        String typeName = request.getParameter("typeName");
        String args = request.getParameter("args");
        String keyWord = request.getParameter("keyWord");

        if (keyWord!=null&&keyWord.length()>0)
            classify.setKeyWord(keyWord);

        if (typeName!=null&&typeName.length()>0)
           classify.setTypeId(new ProductTypeDaoImpl().findByName(typeName).getId());

        if (args!=null&&args.length()>0)
            classify.setBrands(args.split(","));

        response.getWriter().write(JSON.toJSONString(service.findProducts(classify)));
    }

    public void findSimilarGoodsByID(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=utf-8");
        String id = request.getParameter("id");
        response.getWriter().write(JSON.toJSONString(service.findSimilarGoodsByID(id)));
    }
}
